﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace RetrieveEntityDetails.BusinessLogic
{

    public enum EntityTableColumns
    {
        EntityName,
        EntityID,
        AttributeID,
        AttributeName,
        AttributeType,
        EntitySchemaName,
        AttributeSchemaName,
        Description,
        Set,
        Get,
        Checked
    }

    public enum PickListTableColumns
    {
        EntityID,
        AttributeID,
        PickListOptionDisplayName,
        PickListOptionValue,
    }    

    class DataTableManager
    {
        DataTable AttributeMetadataTable;
        DataTable PickListMetadataTable;
        DataTable StateCodeTable;
        DataTable StatusCodeTable;
        DataTable EntityMetadataTable;
        DataTable LookupTypeTable;

        public EntityMetadata EntityMetadataDataSet;
        DataRow Row;

        public Guid EntityID;
        public Guid AttributeID;

        public string EntityName;
        public string AttributeName;
        public string AttributeType;
        public string EntitySchemaName;
        public string AttributeSchemaName;
        public string PickListOptionDisplayName;
        public string StateCodeDisplayName;
        public string StatusCodeDisplayName;
        public string Description;
        public string PrimaryFieldName;
        public string PrimaryFieldSchemaName;
        public string LookupType;

        public bool Checked;
        public bool Set;
        public bool Get;

        public int PickListOptionValue;
        public int StateCodeValue;
        public int StatusCodeValue;

        public DataTableManager()
        {
            EntityMetadataDataSet = new EntityMetadata();    
            AttributeMetadataTable = EntityMetadataDataSet.AttributeMetadataTable;
            PickListMetadataTable = EntityMetadataDataSet.PickListMetadataTable;
            StateCodeTable = EntityMetadataDataSet.StateCodeTable;
            StatusCodeTable = EntityMetadataDataSet.StatusCodeTable;
            EntityMetadataTable = EntityMetadataDataSet.EntityMetadataTable;
            LookupTypeTable = EntityMetadataDataSet.LookupTypeTable;
        }

        public void InsertAttributeRow()
        {
            Row = AttributeMetadataTable.NewRow();
            Row[EntityMetadataDataSet.AttributeMetadataTable.EntityIDColumn.ColumnName] = EntityID;
            Row[EntityMetadataDataSet.AttributeMetadataTable.AttributeIDColumn.ColumnName] = AttributeID;            
            Row[EntityMetadataDataSet.AttributeMetadataTable.AttributeNameColumn.ColumnName] = AttributeName;
            Row[EntityMetadataDataSet.AttributeMetadataTable.AttributeTypeColumn.ColumnName] = AttributeType;
            Row[EntityMetadataDataSet.AttributeMetadataTable.AttributeSchemaNameColumn.ColumnName] = AttributeSchemaName;
            Row[EntityMetadataDataSet.AttributeMetadataTable.EntitySchemaNameColumn.ColumnName] = EntitySchemaName;
            Row[EntityMetadataDataSet.AttributeMetadataTable.DescriptionColumn.ColumnName] = Description;
            AttributeMetadataTable.Rows.Add(Row);
        }

        public void UpdateAttributeRow(string FieldName, Guid UID, bool Checked)
        {
            DataRow[] ResultRows = AttributeMetadataTable.Select(FieldName + " = '" + UID.ToString() + "'");
            if (FieldName == EntityMetadataDataSet.AttributeMetadataTable.AttributeIDColumn.ColumnName)
            {
                if (ResultRows[0][EntityMetadataDataSet.AttributeMetadataTable.AttributeTypeColumn.ColumnName].ToString() != Microsoft.Crm.Sdk.Metadata.AttributeType.PrimaryKey.ToString())
                    ResultRows[0][EntityMetadataDataSet.AttributeMetadataTable.CheckedColumn.ColumnName] = Checked;
            }
            foreach (DataRow ResultRow in ResultRows)
            {
                if (ResultRow[EntityMetadataDataSet.AttributeMetadataTable.AttributeTypeColumn.ColumnName].ToString() != Microsoft.Crm.Sdk.Metadata.AttributeType.PrimaryKey.ToString())
                    ResultRow[EntityMetadataDataSet.AttributeMetadataTable.CheckedColumn.ColumnName] = Checked;
            }
        }

        public DataRow[] FetchCheckedEntityAttributes(Guid EntityID)
        {
            DataRow[] ResultRows = AttributeMetadataTable.Select(EntityMetadataDataSet.AttributeMetadataTable.EntityIDColumn.ColumnName + "= '" + EntityID.ToString() + "'");
            return ResultRows;
        }

        public void InsertPickListRow()
        {
            Row = PickListMetadataTable.NewRow();
            Row[EntityMetadataDataSet.PickListMetadataTable.AttributeIDColumn.ColumnName] = AttributeID;
            Row[EntityMetadataDataSet.PickListMetadataTable.PickListOptionDisplayNameColumn.ColumnName] = PickListOptionDisplayName;
            Row[EntityMetadataDataSet.PickListMetadataTable.PickListOptionValueColumn.ColumnName] = PickListOptionValue;
            Row[EntityMetadataDataSet.PickListMetadataTable.EntityIDColumn.ColumnName] = EntityID;
            PickListMetadataTable.Rows.Add(Row);
        }

        public DataRow[] FetchPickList(Guid EntityID)
        {
            DataRow[] ResultRows = PickListMetadataTable.Select(EntityMetadataDataSet.PickListMetadataTable.EntityIDColumn.ColumnName + " = '" + EntityID.ToString() + "'");
            return ResultRows;
        }

        public void InsertStateCodeRow()
        {
            Row = StateCodeTable.NewRow();
            Row[EntityMetadataDataSet.StateCodeTable.AttributeIDColumn.ColumnName] = AttributeID;
            Row[EntityMetadataDataSet.StateCodeTable.DisplayNameColumn.ColumnName] = StateCodeDisplayName;
            Row[EntityMetadataDataSet.StateCodeTable.ValueColumn.ColumnName] = StateCodeValue;
            Row[EntityMetadataDataSet.StateCodeTable.EntityIDColumn.ColumnName] = EntityID;
            StateCodeTable.Rows.Add(Row);
        }

        public DataRow[] FetchStateCode(Guid EntityID)
        {
            DataRow[] ResultRows = StateCodeTable.Select(EntityMetadataDataSet.StateCodeTable.EntityIDColumn.ColumnName + " = '" + EntityID.ToString() + "'");
            return ResultRows;
        }

        public void InsertStatusCodeRow()
        {
            Row = StatusCodeTable.NewRow();
            Row[EntityMetadataDataSet.StatusCodeTable.AttributeIDColumn.ColumnName] = AttributeID;
            Row[EntityMetadataDataSet.StatusCodeTable.DisplayNameColumn.ColumnName] = StatusCodeDisplayName;
            Row[EntityMetadataDataSet.StatusCodeTable.ValueColumn.ColumnName] = StatusCodeValue;
            Row[EntityMetadataDataSet.StatusCodeTable.EntityIDColumn.ColumnName] = EntityID;
            StatusCodeTable.Rows.Add(Row);
        }

        public DataRow[] FetchStatusCode(Guid EntityID)
        {
            DataRow[] ResultRows = StatusCodeTable.Select(EntityMetadataDataSet.StatusCodeTable.EntityIDColumn.ColumnName + " = '" + EntityID.ToString() + "'");
            return ResultRows;
        }

        public void UpdateAttributeGetSetRow(string FieldName, string GetorSet, Guid UID, bool Switch)
        {
            DataRow[] ResultRows = AttributeMetadataTable.Select(FieldName + " = '" + UID.ToString() + "'");
            if (ResultRows[0][EntityMetadataDataSet.AttributeMetadataTable.AttributeTypeColumn.ColumnName].ToString() != Microsoft.Crm.Sdk.Metadata.AttributeType.PrimaryKey.ToString())
            ResultRows[0][GetorSet] = Switch;
        }

        public void InsertEntityRow()
        {
            Row = EntityMetadataTable.NewRow();
            Row[EntityMetadataDataSet.EntityMetadataTable.EntityIDColumn.ColumnName] = EntityID;
            Row[EntityMetadataDataSet.EntityMetadataTable.PrimaryFieldNameColumn.ColumnName] = PrimaryFieldName;
            Row[EntityMetadataDataSet.EntityMetadataTable.PrimaryFieldSchemaNameColumn.ColumnName] = PrimaryFieldSchemaName;
            Row[EntityMetadataDataSet.EntityMetadataTable.EntityNameColumn.ColumnName] = EntityName;
            Row[EntityMetadataDataSet.EntityMetadataTable.EntitySchemaNameColumn.ColumnName] = EntitySchemaName;
            EntityMetadataTable.Rows.Add(Row);
        }

        public string FetchEntitySchemaName(Guid EntityID)
        {
            DataRow[] ResultRows = EntityMetadataTable.Select(EntityMetadataDataSet.EntityMetadataTable.EntityIDColumn.ColumnName + " = '" + EntityID.ToString() + "'");
            return ResultRows[0][EntityMetadataDataSet.EntityMetadataTable.EntitySchemaNameColumn.ColumnName].ToString();
        }

        public string FetchPrimaryFieldName(Guid EntityID)
        {
            DataRow[] ResultRows = EntityMetadataTable.Select(EntityMetadataDataSet.EntityMetadataTable.EntityIDColumn.ColumnName + " = '" + EntityID.ToString() + "'");
            return ResultRows[0][EntityMetadataDataSet.EntityMetadataTable.PrimaryFieldNameColumn.ColumnName].ToString();
        }

        public string FetchPrimaryFieldSchemaName(Guid EntityID)
        {
            DataRow[] ResultRows = EntityMetadataTable.Select(EntityMetadataDataSet.EntityMetadataTable.EntityIDColumn.ColumnName + " = '" + EntityID.ToString() + "'");
            return ResultRows[0][EntityMetadataDataSet.EntityMetadataTable.PrimaryFieldSchemaNameColumn.ColumnName].ToString();
        }

        public void InsertLookupTypeRow()
        {
            Row = LookupTypeTable.NewRow();
            Row[EntityMetadataDataSet.LookupTypeTable.EntityIDColumn.ColumnName] = EntityID;
            Row[EntityMetadataDataSet.LookupTypeTable.AttributeIDColumn.ColumnName] = AttributeID;
            Row[EntityMetadataDataSet.LookupTypeTable.LookupTypeColumn.ColumnName] = LookupType;
            LookupTypeTable.Rows.Add(Row);
        }

        public DataRow[] FetchLookupTypes(Guid EntityID)
        {
            DataRow[] ResultRows = LookupTypeTable.Select(EntityMetadataDataSet.LookupTypeTable.EntityIDColumn.ColumnName + " = '" + EntityID.ToString() + "'");
            return ResultRows;
        }           
    }
}
